package com.qf.web.sys_role.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.utils.ResultUtil;
import com.qf.utils.ResultVo;
import com.qf.web.sys_role.entity.Role;
import com.qf.web.sys_role.entity.RolePrams;
import com.qf.web.sys_role.entity.SaveRoleParams;
import com.qf.web.sys_role.service.RoleService;
import com.qf.web.sys_role_menu.service.RoleMenuService;
import com.qf.web.sys_user.entity.AssignParams;
import com.qf.web.sys_user.entity.AssignVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author xufuhan
 * @Date 2024/9/10 16:41
 */
@RestController
@RequestMapping("/api/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @Autowired
    private RoleMenuService roleMenuService;
    //新增
    @PostMapping
    public ResultVo addRole(@RequestBody Role role){
        boolean save = roleService.save(role);
        if (save){
            return ResultUtil.success("新增角色成功");
        }
        return ResultUtil.success("新增角色失败");
    }
    //编辑
    @PutMapping
    public ResultVo editRole(@RequestBody Role role){
        boolean save = roleService.updateById(role);
        if (save){
            return ResultUtil.success("编辑角色成功");
        }
        return ResultUtil.success("编辑角色失败");
    }
    //删除
    @DeleteMapping("/{roleId}")
    public ResultVo deleteRole(@PathVariable ("roleId") Long roleId){
        boolean remove = roleService.removeById(roleId);
        if (remove){
            return ResultUtil.success("删除角色成功");
        }
        return ResultUtil.success("删除角色失败");
    }
    //角色列表
    @GetMapping("/list")
    public ResultVo getList(RolePrams prams){
        IPage<Role> list = roleService.list(prams);
        return ResultUtil.success("查询成功",list);
    }

    //分配权限回显
    @GetMapping("/getAssignShow")
    public ResultVo getAssignShow(AssignParams params){
        AssignVo assignShow = roleService.getAssignShow(params);
        return ResultUtil.success("查询成功",assignShow);
    }

    //
    @PostMapping ("/saveRoleMenu")
    public ResultVo saveRoleMenu(@RequestBody SaveRoleParams params){
        roleMenuService.saveRoleMenu(params.getRoleId(), params.getList());
        return ResultUtil.success("分配权限成功");
    }
}
